
package com.budwk.app.sqfw.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.annotation.ApiIdempotent;
import com.budwk.app.sqfw.component.PushMsgComponent;
import com.budwk.app.sqfw.models.SqfwPushMsgLog;
import com.budwk.app.sqfw.services.SqfwPushMsgLogService;
import com.budwk.app.sqfw.vo.pushmsg.InOutPushVo;
import com.budwk.app.sqfw.vo.pushmsg.PushMsgConfigVo;
import com.budwk.app.sys.models.Sys_unit;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.starter.common.constant.RedisConstant;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.openapi.annotation.ApiResponses;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.log.annotation.SLog;
import lombok.extern.slf4j.Slf4j;
import org.nutz.integration.jedis.RedisService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @title: SendNotifyMessageController
 * @Author Tan
 * @Date: 2023/7/21 9:46
 * @Version 1.0
 */
@IocBean
@At("/platform/sqfw/pus/msg")
@ApiDefinition(tag = "消息推送")
@Slf4j
public class PushMsgController {

    @Inject
    private PushMsgComponent pushMsgComponent;
    @Inject
    private SqfwPushMsgLogService pushMsgLogService;
    @Inject
    private SysUserService userService;
    @Inject
    private RedisService redisService;

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "人工处理发送失败的消息(失败的消息重试接口)(vivo、oppo、huawei、xiaomi) ")
    @ApiResponses
    @SaCheckLogin
    public Result<?> failMsgRetry(@Param("msgId") String msgId) {
        return pushMsgLogService.failMsgRetry(msgId);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "发送消息")
    @SLog(tag = "发送消息", value = "${args[0]}")
    @ApiResponses
    @ApiIdempotent
    @SaCheckLogin
    public Result<?> pushAppMessage(@Param("..") SqfwPushMsgLog msgLog) {
        log.info("PushMsgController pushAppMessage param: {}", JSONObject.toJSONString(msgLog));
        pushMsgLogService.pushAppMessage(msgLog);
        return Result.success("发送成功，请稍后查看发送结果！");
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "消息推送（按照五级向下推送信息）")
    @SLog(tag = "消息推送（按照五级向下推送信息）", value = "${args[0]}")
    @ApiResponses
    @ApiIdempotent
    @SaCheckLogin
    public Result<?> pushPowerMessage(@Param("..") SqfwPushMsgLog msgLog) {
        pushMsgLogService.pushPowerMessage(msgLog);
        return Result.success();
    }


    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "消息回复")
    @ApiResponses
    @SaCheckLogin
    public Result<?> replyMessage(@Param("..") SqfwPushMsgLog msgLog) {
        log.info("PushMsgController replyMessage param: {}", JSONObject.toJSONString(msgLog));
        pushMsgLogService.replyMessage(msgLog);
        return Result.success();
    }

    @At
    @Ok("json")
    @DELETE
    @ApiOperation(name = "消息删除")
    @SLog(tag = "消息删除", value = "${args[0]}")
    @ApiResponses
    @SaCheckLogin
    public Result<?> deleteMessage(@Param("msgId") String msgId, @Param("pushBatchNo") String pushBatchNo) {
        log.info("PushMsgController deleteMessage param: {}, pushBatchNo: {}", msgId, pushBatchNo);
        pushMsgLogService.deleteMessage(msgId, pushBatchNo);
        return Result.success();
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "消息详情")
    @ApiResponses
    @SaCheckLogin
    public Result<?> getMsgDetail(@Param("msgId") String msgId) {
        log.info("PushMsgController getMsgDetail param: {}", msgId);
        SqfwPushMsgLog msgLog = pushMsgLogService.fetch(msgId);
        msgLog.setRead(true).setUpdatedAt(System.currentTimeMillis());
        pushMsgLogService.updateIgnoreNull(msgLog);
        return Result.data(msgLog);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询消息列表")
    @ApiResponses
    @SaCheckLogin
    public Result<?> listMsgByPage(@Param("..") SqfwPushMsgLog msgLog) {
        log.info("PushMsgController listMsgByPage param: {}", JSONObject.toJSONString(msgLog));
        Pagination p = pushMsgLogService.listMsgByPage(msgLog);
        return Result.data(p);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "统计收件箱和发件箱的数量")
    @ApiResponses
    @SaCheckLogin
    public Result<?> statisticsInOutboxNumber() {
        InOutPushVo vo = pushMsgLogService.statisticsInOutboxNumber();
        return Result.data(vo);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "查询单位下的所有人员信息")
    @ApiResponses
    @SaCheckLogin
    public Result<List<Sys_user>> queryUnitAllUser(@Param("unitId") String unitId) {
        return Result.data(pushMsgLogService.queryUnitAllUser(unitId));
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "按照渠道进行消息推送测试")
    @ApiResponses
    @SaCheckLogin
    public Result<?> pushMessageTest(@Param("pushChannel") String pushChannel,
                                     @Param("pushToken") String pushToken,
                                     @Param("fwId")String fwId) {
        Map mapToken = new HashMap();
        mapToken.put("pushChannel" , pushChannel);
        mapToken.put("pushToken" , pushToken);
        Sys_user user = userService.fetch("5c8e522eba5e468bbfafc43b0a721703");
        PushMsgConfigVo c = PushMsgComponent.buildPushMsgConfigVo(mapToken, fwId, "msg_cc", null);
        pushMsgLogService.pushMessage(c, user,null);
        return Result.success();
    }
}


